
/*
'===============================================================================
'  MP.LayerHelper.MP_OBS.BL.MenuManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.FactoryClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MP.Core.Library;

namespace MP.LayerHelper.MP_OBS.BLL
{
	public class MenuManager : MenuManagerBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public MenuManager()
		{
			// Nothing for now.
		}
		
		/// <summary>
		/// Method to create an instance of MenuManager
		/// </summary>
		/// <returns>An instant of MenuManager class</returns>
		public static MenuManager CreateInstant()
		{
			return new MenuManager();
		}

        public EntityCollection<MenuEntity> GetByParentId(string ParentId)
        {
            EntityCollection<MenuEntity> items = new EntityCollection<MenuEntity>();
            IPredicateExpression predicate = new PredicateExpression();
            predicate.Add(MenuFields.ParentId == ParentId);
            RelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(predicate);
            SortExpression sort = new SortExpression();
            sort.Add(MenuFields.OrderIndex | SortOperator.Ascending);
            sort.Add(MenuFields.MenuName | SortOperator.Ascending);

            using (DataAccessAdapterBase adapter = new DataAccessAdapterFactory().CreateAdapter())
            {
                adapter.FetchEntityCollection(items, filter, 0, sort);
            }

            return items;
        }

        public DataSet GetMenuByParentId(string ParentId)
        {
            string strSQL = "prcGetMenu";
            SqlParameter param = new SqlParameter("@ParentId", ParentId);
            return SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringOBS, CommandType.StoredProcedure, strSQL, param);
        }

        public DataSet GetMenuParent(params string[] menuId)
        {
            string strSQL = "Select * " +
                            "From Menu " +
                            "Where IsEnable=1 ";
            string tmpSQL = "";
            for (int i = 0; i < menuId.Length; i++)
                tmpSQL += " or Id='" + menuId[i] + "' ";
            if (!string.IsNullOrEmpty(tmpSQL))
                strSQL += " and ( " + tmpSQL.Substring(3) + ") ";
            strSQL += "Order By OrderIndex ";

            return SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL);
        }

        public void UpdateMenu(string menuId, string menuName, string url, int OrderIndex, bool isEnable,string parentId)
        {
            string strSQL = "Update Menu " +
                            "Set MenuName=@MenuName, Url=@Url, OrderIndex=@OrderIndex, IsEnable=@IsEnable, ParentId=@ParentId " +
                            "Where Id=@MenuId";
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@MenuName", menuName);
            param[1] = new SqlParameter("@Url", url);
            param[2] = new SqlParameter("@OrderIndex", OrderIndex);
            param[3] = new SqlParameter("@IsEnable", isEnable);
            param[4] = new SqlParameter("@MenuId", menuId);
            param[5] = new SqlParameter("@ParentId", parentId);
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL, param);
        }
	}
}
